package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.exception.BaseException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询菜品和对应的口味数据
     * @param id
     * @return
     */
    @Override
    public DishDTO getByIdWithFlavor(Long id) {
        return dishMapper.getByIdWithFlavor(id);
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public DishDTO[] list(Long categoryId) {
        log.info("管理页查询");
        DishDTO[] byCategoryId = dishMapper.getByCategoryId(categoryId);
        return byCategoryId;
    }

    /**
     * 根据分类id和菜品状态查询菜品
     * @param dish
     * @return
     */
    @Override
    public DishDTO[] list(Dish dish) {
        DishDTO[] byCategoryId = dishMapper.getByCategoryIdWithStatus(dish);
        return byCategoryId;
    }

    /**
     * 根据id查询菜品状态
     * @param dishId
     * @param status
     * @return
     */
    @Override
    public String getByIdStatus(Long dishId, Integer status) {
        return dishMapper.getByIdStatus(dishId,status);
    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    @Transactional
//    @AutoFill(OperationType.INSERT)
    public void saveWithFlavor(DishDTO dishDTO) {

        // 拷贝菜品基本信息，
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        // 向菜品表插入一条数据
        dishMapper.save(dish);
        // 获取插入的菜品id
        Long dishId = dish.getId();

        // 保存菜品口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            // 向口味表插入n条数据
            dishFlavorMapper.saveFlavor(flavors);
        }

    }


    /**
     * 菜品起售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        dishMapper.startOrStop(status,id);
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDTO dishDTO) {
        // 拷贝菜品基本信息，
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        // 向菜品表更新一条数据
        dishMapper.update(dish);

        // 修改菜品口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            // 根据菜品ID先清理当前菜品的口味数据
            dishFlavorMapper.deleteByDishId(new Long[]{dish.getId()});
            // 再重新插入新的口味数据
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            // 向口味表插入n条数据
            dishFlavorMapper.saveFlavor(flavors);
        }
    }

    /**
     * 菜品批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void delete(Long[] ids) {
        // 先判断菜品状态
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == 1){
                // 菜品正在售卖中，不能删除
                throw new BaseException(dish.getName() + "菜品正在售卖中，不能删除");
            }
        }
        // 根据菜品ID，先删除菜品口味信息
        dishFlavorMapper.deleteByDishId(ids);
        // 再删除菜品信息
        dishMapper.delete(ids);
    }

}
